package toubiao.service.impl.init;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import toubiao.dao.support.ResourceDaoI;
import toubiao.dao.support.RoleDaoI;
import toubiao.domain.po.manage.PoResource;
import toubiao.domain.po.manage.PoRole;
import toubiao.service.init.InitRoleServiceI;

@Service
public class InitRoleServiceImpl  implements InitRoleServiceI{

	@Autowired
	private ResourceDaoI resourceDao;
	
	@Autowired
	private RoleDaoI roleDao;
	
	@Override
	public void  clearAndInit() {
		// TODO Auto-generated method stub
		clearRole();
		init();
	}
	
	@Override
	public void init(){
		initAdminRole();
	}
	
	@Override
	public void clearRole(){
		String hql="from PoRole";
		List<PoRole> roleList=roleDao.find(hql);
		for(PoRole role:roleList){
			roleDao.delete(role);
		}
	}
	
	/**
	    * @Title: initRole
	    * @Description: 初始化超级管理角色，拥有所有权限
	    * @param     
	    * @return void    
	    * @throws
	    */
	@Override
	public void initAdminRole(){
		//管理角色
		PoRole adminRole=new PoRole();
		adminRole.setName("admin");
		adminRole.setRemark("拥有所有资源");
		List<PoResource> list =resourceDao.getAll(PoResource.class);
		Set<PoResource> resourceSet=new HashSet<>();
		resourceSet.addAll(list);
		adminRole.setTresourceSet(resourceSet);
		roleDao.save(adminRole);
	}
}
